from __future__ import annotations

from pydantic import BaseModel, Field
from typing import Optional, List


class DirectoryNode(BaseModel):
    """目录结构节点"""

    name: str = Field(..., min_length=1, description="目录名称")
    description: Optional[str] = Field(None, description="目录或文件用途说明")
    files: List[str] = Field(default_factory=list, description="该目录下需要创建的文件列表")
    children: List['DirectoryNode'] = Field(default_factory=list, description="子目录节点")

class ProjectConfig(BaseModel):
    """
    定义项目生成的请求体模型，与前端表单对应。
    """
    projectName: str = Field(..., min_length=3, description="项目名称，必填")
    projectType: str = Field(..., description="项目类型，例如 'Web', 'App', 'MiniProgram'")
    uiFramework: str = Field(..., description="选择的UI框架，例如 'React', 'Vue'")
    language: Optional[str] = Field('typescript', description="编程语言，例如 'typescript' 或 'javascript'")
    buildTool: Optional[str] = Field('vite', description="构建工具，例如 'vite' 或 'webpack'")
    
    # 目标平台选择（单选）
    targetPlatform: Optional[str] = Field('pc', description="目标平台，单选: pc | app | miniprogram")
    
    # 基础组件选择
    baseComponents: List[str] = Field(default_factory=list, description="基础组件列表，如react-dom, react-router-dom等")
    extendedComponents: List[str] = Field(default_factory=list, description="扩展组件或推荐组件列表")
    
    # 以下为可选字段
    description: Optional[str] = Field(None, max_length=500, description="项目描述")
    maxLevels: Optional[int] = Field(2, ge=1, le=10, description="最大菜单层级")
    customRequirements: Optional[str] = Field(None, description="自定义需求")
    requirementDescription: Optional[str] = Field(None, description="AI 分析使用的需求描述")

    # 架构相关配置
    stateManagement: Optional[str] = Field(None, description="状态管理方案")
    router: Optional[str] = Field(None, description="路由方案")
    layout: Optional[str] = Field(None, description="布局类型")
    devProxy: Optional[str] = Field(None, description="开发环境代理地址")
    testProxy: Optional[str] = Field(None, description="测试环境代理地址")
    prodProxy: Optional[str] = Field(None, description="生产环境代理地址")
    additionalConfig: Optional[str] = Field(None, description="其他架构配置")

    directoryStructure: List[DirectoryNode] = Field(default_factory=list, description="AI 生成的项目目录结构")
    directorySummary: Optional[str] = Field(None, description="目录结构分析摘要")

class GenerationResponse(BaseModel):
    """
    定义项目生成成功后的响应模型。
    """
    status: str = "success"
    message: str
    project_path: str


DirectoryNode.model_rebuild()
